package com.example.demo.study.supermarket2;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

// 会员管理类
public class MemberMgr {
    // 所有会员（卡） --- 因为会员 会比较多 --- 所以会用到集合 -- list
    List<Member> memberList = new ArrayList<Member>();

    // 用户输入内容
    static Scanner scanner = new Scanner(System.in);
    // 输入欢迎菜单
    public void menu(){
        System.out.println("***********欢迎使用超市会员管理系统*****************************");
        System.out.println("1.开卡  2.修改密码  3.积分累计  4.积分兑换  5.查询剩余积分  6.退出");
        System.out.println("************************************************************");
        System.out.print("请输入您的选择：");
    }

    // 框架搭建流程
    public void start(){
        Boolean flag = true;
            do{
                menu();
                int choice = scanner.nextInt();
                switch (choice){
                    case 1:
                        // 开卡
                        System.out.println("开卡：");
                        openCard();
                        break;
                    case 2:
                        // 修改密码
                        System.out.println("修改密码：");
                        modifyPassword();
                        break;
                    case 3:
                        // 积分累计
                        System.out.println("积分累计：");
                        accumulateScore();
                        break;
                    case 4:
                        // 积分兑换
                        System.out.println("积分兑换：");
                        integralExchange();
                        break;
                    case 5:
                        // 查询剩余积分
                        System.out.println("查询剩余积分：");
                        integralInquiry();
                        break;
                    case 6:
                        // 退出
                        System.out.println("退出：");
                        flag = false;
                        break;
                    default:
                        System.out.println("输入有误，请重新输入！");
                        break;
                }
            }while (flag);
    }

    // 开卡方法
    public void openCard(){

        Member member = new Member();
        System.out.print("请输入注册姓名：");
        String cardName = scanner.next();
        System.out.print("请输入注册密码：");
        String cardPwd = scanner.next();
        StringBuffer sbPwd= new StringBuffer(cardPwd);
        if(sbPwd.length()<6){
            System.out.println("密码长度小于6位，请重新输入注册密码！");
            return;
        }else {
            // 随机生成会员卡号
            Random random = new Random();
            int cardNo = 10000000 + random.nextInt(90000000);
            // 生成今当日期
            LocalDate localDate = LocalDate.now();
            String regDate = localDate.getMonthValue()+"月"+localDate.getDayOfMonth()+"日";
            member.setName(cardName);
            member.setCardId(cardNo);
            member.setPassword(sbPwd.toString());
            member.setScore(100);
            member.setRegisterDate(regDate);
            memberList.add(member);

            System.out.println("开通会员卡成功！系统赠送您100积分，你的会员卡号为："+cardNo);
            System.out.println("会员注册人数:"+memberList.size());


        }


//        System.out.println("开卡成功！");
    }
    // 修改密码方法
    public  void modifyPassword(){
        System.out.println("请输入会员卡号：");
        int cartId = scanner.nextInt();
        for (Member member: memberList) {
            if(member.getCardId()== cartId){
                System.out.println("请输入新密码：");
                String oldPwd = scanner.next();
                if(oldPwd.length()<6){
                    System.out.println("密码长度不能小于6位，请重新输入！");
                    return;
                }else {
                    member.setPassword(oldPwd);
                    System.out.println("密码修改成功！");
                    return;
                }
            }
        }

    }
    // 积分累计
    public void accumulateScore(){
        System.out.println("请输入您的会员卡号：");
        int cardId = scanner.nextInt();
        System.out.println("请输入您的会员卡密码：");
        String cardPwd = scanner.next();
        for (Member member: memberList) {
            if(member.getCardId()== cardId && member.getPassword().equals(cardPwd) ){
                System.out.println("请输入您要积分的数量：");
                int score = scanner.nextInt();
                member.setScore(member.getScore()+score);
                System.out.println("积分累计成功！累计共金额："+member.getScore());
                return;
            }
        }

    }
    // 积分兑换方法
    public void integralExchange(){
        System.out.println("请输入您的会员卡号：");
        int cardId = scanner.nextInt();
        System.out.println("请输入您的会员卡密码：");
        String cardPwd = scanner.next();
        for (Member member: memberList) {
            if (member.getCardId() == cardId && member.getPassword().equals(cardPwd)) {
                System.out.println("请输入您需要兑换使用的积分（100积分抵用0.1，不足100积分不做抵用）：");
                int score = scanner.nextInt();
                 if(score>=100){
                    double price = score*0.001;
                     System.out.println("您的消费金额中使用会员积分抵消："+price+"元");
                    System.out.println("积分兑换成功!");
                    member.setScore(member.getScore()-score);
                 }else {
                     System.out.println("积分不足，兑换失败！");
                 }
                return;
            }
        }

    }
    // 积分查询方法
    public void integralInquiry(){
        System.out.println("请输入您的会员卡号：");
        int cardId = scanner.nextInt();
        System.out.println("请输入您的会员卡密码：");
        String cardPwd = scanner.next();
        for (Member member: memberList) {
            if(member.getCardId()== cardId && member.getPassword().equals(cardPwd) ){
               System.out.print("姓名："+member.getName());
               System.out.println();
               System.out.print("会员卡呈："+member.getCardId());
               System.out.println();
               System.out.print("剩余积分："+member.getScore());
               System.out.println();
               System.out.print("开卡日期："+member.getRegisterDate());
               System.out.println();
               return;
            }
        }
    }



}
